package com.hr.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hr.constant.JwtClaimsConstant;
import com.hr.constant.MessageConstant;
import com.hr.domain.dto.UserLoginDTO;
import com.hr.domain.po.User;
import com.hr.exception.LoginException;
import com.hr.mapper.UserMapper;
import com.hr.properties.JwtProperties;
import com.hr.service.IUserService;
import com.hr.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LuoQiu
 * @since 2024-09-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private JwtProperties jwtProperties;

    @Override
    public String loginInfo(UserLoginDTO userLoginDTO) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getMobile, userLoginDTO.getMobile());
        User userInfo = super.getOne(userLambdaQueryWrapper);
        if (ObjectUtil.isEmpty(userInfo)) {
           throw new LoginException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!ObjectUtil.equal(userLoginDTO.getPassword(), userInfo.getPassword())) {
            throw new LoginException(MessageConstant.PASSWORD_ERROR);
        }
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, userInfo.getId());
        return JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
    }
}
